<template>
  <header class="header">
    <h1>todos</h1>
    <input
      id="toggle-all"
      class="toggle-all"
      :checked="isAll"
      @click="listAll"
      type="checkbox"
    />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      @keydown.enter="enter"
      placeholder="输入任务名称-回车确认"
      autofocus
    />
  </header>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  data() {
    return {}
  },
  computed: {
    // 拿到 tlist 数据
    ...mapState('ToDos', ['tlist']),
    ...mapGetters('ToDos', ['isAll'])
  },
  methods: {
    // 回车追加数据
    enter(e) {
      if (e.target.value.length === 0) return alert('请输入内容')
      this.$store.commit('ToDos/listPush', {
        id:
          this.tlist.length > 0
            ? this.tlist[this.tlist.length - 1].id + 1
            : 100,
        isDone: false,
        name: e.target.value
      })
      e.target.value = ''
    },
    // 全选
    listAll(e) {
      this.$store.commit('ToDos/listAlls', e.target.checked)
    }
  }
}
</script>
